package com.example.demo.service;

import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    
    @Autowired
    private StudentMapper studentMapper;
    
    // 查询所有学生
    public List<Student> findAll() {
        return studentMapper.findAll();
    }
    
    // 根据ID查询学生
    public Student findById(Long id) {
        return studentMapper.findById(id);
    }
    
    // 保存学生信息
    public boolean save(Student student) {
        if (student.getId() == null) {
            // 新增
            return studentMapper.insert(student) > 0;
        } else {
            // 更新
            return studentMapper.update(student) > 0;
        }
    }
    
    // 根据ID删除学生
    public boolean deleteById(Long id) {
        return studentMapper.deleteById(id) > 0;
    }
    
    // 根据姓名模糊查询
    public List<Student> findByName(String name) {
        return studentMapper.findByName(name);
    }
}